install.packages('ggplot2')
install.packages('ggeffects')
install.packages('lme4')
install.packages('sjPlot')
library(ggplot2)
library(ggeffects)
library(lme4)
library(sjPlot)

load('figure1_data.rdata')
load('whip_count_model_data.rdata')
load('new_merged_data.rdata')


#### Figure 1 ####

cmte_vs_floor_plot <- ggplot(floor_and_cmte_minority_roll_rates_long, aes(x = congress, y = minority_roll_rate, group = venue)) + geom_line(aes(linetype = venue)) + labs(x = 'Congress', y = 'Minority Party Roll Rate') + scale_x_continuous(breaks = seq(104,114, by = 1)) + scale_linetype_discrete(name = 'Venue') + theme_bw() + theme(legend.position = 'bottom')


#### Whip model ####

whip_model <- glmer(whip ~ (1|congress) + (1|primary_cmte) + bipartisan_cosponsors20 + minority_or_dissenting_views + majority_sponsor + reported_favorably, family = binomial (link = 'logit'), data = test105_107, glmerControl(optim = 'bobyqa'))

#### Predicted probabilities from whip model ####

pr_views <- ggpredict(whip_model, "minority_or_dissenting_views", type = "random", condition = c(bipartisan_cosponsors20 = 0, majority_sponsor = 1, reported_favorably = 1))
pr_views

pr_cosponsors <- ggpredict(whip_model, "bipartisan_cosponsors20", type = "random", condition = c(minority_or_dissenting_views = 0, majority_sponsor = 1, reported_favorably = 1))
pr_cosponsors

pr_maj_sponsor <- ggpredict(whip_model, "majority_sponsor", type = "random", condition = c(bipartisan_cosponsors20 = 0, minority_or_dissenting_views = 0, reported_favorably = 1))
pr_maj_sponsor

pr_reported <- ggpredict(whip_model, "reported_favorably", type = "random", condition = c(bipartisan_cosponsors20 = 0, majority_sponsor = 1, minority_or_dissenting_views = 0))
pr_reported

#### Table 2 Model 1 ####

table2_model1 <- glmer(floor ~ (1|sponsor) + (1|congress) + (1|primary_cmte) + reported_favorably + bipartisan_cosponsors20 + minority_or_dissenting_views + commemorative + majority_sponsor + mult_ref, family = binomial (link = 'logit'), data = new_merged_data, glmerControl(optim = 'bobyqa'))

#### Predicted probabilities from Table 2 Model 1 ####

pr_views <- ggpredict(table2_model1, "minority_or_dissenting_views", type = "random", condition = c(bipartisan_cosponsors20 = 1, majority_sponsor = 1, reported_favorably = 1, commemorative = 0, mult_ref = 0))
pr_views

pr_report <- ggpredict(table2_model1, "reported_favorably", type = "random", condition = c(bipartisan_cosponsors20 = 1, majority_sponsor = 1, minority_or_dissenting_views = 0, commemorative = 0, mult_ref = 0))
pr_report

pr_bipartisan <- ggpredict(table2_model1, "bipartisan_cosponsors20", type = "random", condition = c(reported_favorably = 1, majority_sponsor = 1, minority_or_dissenting_views = 0, commemorative = 0, mult_ref = 0))
pr_bipartisan

pr_maj_sponsor <- ggpredict(table2_model1, "majority_sponsor", type = "random", condition = c(reported_favorably = 1, bipartisan_cosponsors20 = 1, minority_or_dissenting_views = 0, commemorative = 0, mult_ref = 0))
pr_maj_sponsor

pr_mult_ref <- ggpredict(table2_model1, "mult_ref", type = "random", condition = c(reported_favorably = 1, bipartisan_cosponsors20 = 1, minority_or_dissenting_views = 0, commemorative = 0, majority_sponsor = 1))
pr_mult_ref

pr_commemorative <- ggpredict(table2_model1, "commemorative", type = "random", condition = c(reported_favorably = 1, bipartisan_cosponsors20 = 1, minority_or_dissenting_views = 0, mult_ref = 0, majority_sponsor = 1))
pr_commemorative

#### Table 2 Model 2 ####

table2_model2 <- glmer(floor ~ (1|sponsor) + (1|congress) + reported_favorably + bipartisan_cosponsors20 + minority_or_dissenting_views + majority_sponsor + mult_ref, family = binomial (link = 'logit'), data = subset(new_merged_data, new_merged_data$appropriations == 1|new_merged_data$reauthorization == 1|new_merged_data$budget_resolutions == 1), glmerControl(optim = 'bobyqa'))

#### Predicted probabilities from Table 2 Model 2 ####

pr_views <- ggpredict(table2_model2, "minority_or_dissenting_views", type = "random", condition = c(bipartisan_cosponsors20 = 1, majority_sponsor = 1, reported_favorably = 1, mult_ref = 0))
pr_views

pr_report <- ggpredict(table2_model2, "reported_favorably", type = "random", condition = c(bipartisan_cosponsors20 = 1, majority_sponsor = 1, minority_or_dissenting_views = 0, mult_ref = 0))
pr_report

pr_bipartisan <- ggpredict(table2_model2, "bipartisan_cosponsors20", type = "random", condition = c(reported_favorably = 1, majority_sponsor = 1, minority_or_dissenting_views = 0, mult_ref = 0))
pr_bipartisan

pr_maj_sponsor <- ggpredict(table2_model2, "majority_sponsor", type = "random", condition = c(reported_favorably = 1, bipartisan_cosponsors20 = 1, minority_or_dissenting_views = 0, mult_ref = 0))
pr_maj_sponsor

pr_mult_ref <- ggpredict(table2_model2, "mult_ref", type = "random", condition = c(reported_favorably = 1, bipartisan_cosponsors20 = 1, minority_or_dissenting_views = 0, majority_sponsor = 1))
pr_mult_ref



#### Table 3 Model ####

double_interaction_model <- glmer(floor ~ (1|sponsor) + (1|congress) + (1|primary_cmte) + reported_favorably + bipartisan_cosponsors20 + minority_or_dissenting_views + commemorative + majority_sponsor + pct_sorted + mult_ref + maj_pct + minority_or_dissenting_views * maj_pct + minority_or_dissenting_views * pct_sorted, family = binomial (link = 'logit'), data = new_merged_data, glmerControl(optim = 'bobyqa'))


#### Figure 2 ####

double_interaction_model_plot <- plot_model(double_interaction_model, type = 'pred', terms = c('maj_pct', 'minority_or_dissenting_views'), axis.title = c('Majority Party Share of Chamber', 'Probability of Floor Consideration'), title = '', colors = 'bw') + theme_bw() + theme(legend.position = 'bottom')

double_interaction_model_plot$labels$linetype <- 'Minority or Dissenting Views'

double_interaction_model_plot$labels$shape <- 'Minority or Dissenting Views'


#### Figure 3 ####

double_interaction_sortedness_plot <- plot_model(double_interaction_model, type = 'pred', terms = c('pct_sorted', 'minority_or_dissenting_views'), axis.title = c('Percent Sorted Districts', 'Probability of Floor Consideration'), title = '', colors = 'bw') + theme_bw() + theme(legend.position = 'bottom')

double_interaction_sortedness_plot$labels$linetype <- 'Minority or Dissenting Views'

double_interaction_sortedness_plot$labels$shape <- 'Minority or Dissenting Views'



